package co.unal.visor.model;

import co.unal.visor.model.interfaces.IFilter;

public class GreyscaleFilter implements IFilter {

	@Override
	public Image process(Image img) {

		byte[][] stripArray = img.getStripArray();
		int bitspp = img.getBitspp();
		int width = img.getWidth();
		int height = img.getHeight();
		

		int k = 0;
		int j = 0;
		for (int i = 0; i < stripArray.length; i++) {
			byte[] currentStrip = stripArray[i];
			int tempIndex = 0;
			System.out.println("createPixelMatrix() " + i);
			while (k < height) {
				for (j = 0; j < width; j++) {
					if (tempIndex < stripArray[i].length) {

					//	double medRGB = ((currentStrip[tempIndex + 0] * 0.2989
						//		+ currentStrip[tempIndex + 1] * .5870 + currentStrip[tempIndex + 2] * 0.1140));

						double medRGB =((currentStrip[tempIndex + 0] * 0.22126
								+ currentStrip[tempIndex + 1] * .7152 + currentStrip[tempIndex + 2] * 0.0722));
					
						currentStrip[tempIndex + 0] =  (byte) medRGB;
						currentStrip[tempIndex + 1] =  (byte) medRGB;
						currentStrip[tempIndex + 2] =  (byte) medRGB;
						//System.out.println("solo aca: "+currentStrip[tempIndex + 0]);
						tempIndex += (bitspp / 8);
					}
				}
				k++;
			}
			stripArray[i] = currentStrip;
		}
		
		img.setStripArray(stripArray);
		return img;
	}

}
